import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author yu
 * @date 2025年05月21日 11:17
 */
public class _10_gird_03_kama99_bfs_2 {
    public static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; // 顺时针遍历（下右左上）

    public static void bfs(int[][] grid, boolean[][] visited, int x, int y) {
        Queue<int[]> queue = new LinkedList<>(); // 使用 int[] 存储坐标 [x, y]
        queue.offer(new int[]{x, y});
        visited[x][y] = true;//遇到入队直接标记为优先，
        // 否则出队时才标记的话会导致重复访问，比如下方节点会在右下顺序的时候被第二次访问入队

        while (!queue.isEmpty()) {
            int[] cur = queue.poll(); // cur[0]=x, cur[1]=y
            int curX = cur[0];
            int curY = cur[1];

            for (int[] d : dir) {
                int nextX = curX + d[0];
                int nextY = curY + d[1];

                if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) {
                    continue;
                }

                if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
                    visited[nextX][nextY] = true;
                    queue.offer(new int[]{nextX, nextY});
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] grid = new int[m][n];
        boolean[][] visited = new boolean[m][n];
        int ans = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j] && grid[i][j] == 1) {
                    ans++;
                    bfs(grid, visited, i, j);
                }
            }
        }

        System.out.println(ans);
        sc.close();
    }
}

